
package guoan.service;

import guoan.core.common.WebConfig;
import guoan.core.dto.RestResponse;
import guoan.domain.CoachConfig;
import guoan.domain.Keyword;
import guoan.repository.CoachConfigRepository;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(readOnly = false)
public class CoachConfigService {
    protected final Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    private CoachConfigRepository coachConfigRepository;
    @Autowired
    private KeywordService keywordService;

    public CoachConfig findCoachConfig(){
        // TODO Auto-generated method stub
        return coachConfigRepository.findOne(1L);
    }

    public RestResponse<Boolean> update(CoachConfig coachConfig){
        // TODO Auto-generated method stub
        CoachConfig cco = findCoachConfig();
        cco.setContent(coachConfig.getContent());
        String keyName = coachConfig.getKeyName();

        if (!keyName.equals(cco.getKeyName())) {
            keywordService.delete(WebConfig.KEYWORD_KEY_INFO_COACH, cco.getId());
            keywordService.add(keyName, WebConfig.KEYWORD_KEY_INFO_COACH, cco.getId());
            cco.setKeyName(keyName);
        }
        coachConfigRepository.save(cco);

        return RestResponse.ok(true);
    }
}
